package com.dxysun.springboot.filter;

import java.io.IOException;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.dxysun.springboot.wrapper.MyHttpServletRequestWrapper;
import com.dxysun.springboot.wrapper.ResponseWrapper;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.filter.OncePerRequestFilter;

@Configuration
public class MyOncePerRequestFilter extends OncePerRequestFilter
{
	private static final Log logger = LogFactory.getLog(MyOncePerRequestFilter.class);

	@Override
	protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
			throws ServletException, IOException
	{
		MyHttpServletRequestWrapper requestWrapper = new MyHttpServletRequestWrapper(request);

//		requestWrapper.setParameter("msg", "e25hbWU6InF3ZXJ0In0=");
		requestWrapper.setParameter("isEncrypt", "true");
		requestWrapper.putHeader("isEncrypt", "true");
		requestWrapper.putHeader("encryptData", "encryptDataMsg");
//		requestWrapper.getHeader()
		logger.info("MyOncePerRequestFilter before");

//		response.setContentType("application/json");
//		response.setCharacterEncoding("UTF-8");
//		response.getWriter().write("{\"msg\":\"解密异常\"}");
//		response.getWriter().close();


//		response.getOutputStream().write("error".getBytes());
//		response.getOutputStream().flush();
//		response.getOutputStream().close();
		filterChain.doFilter(requestWrapper, response);
		logger.info("MyOncePerRequestFilter after");

	}
}
